﻿Imports MySql.Data.MySqlClient


Public Class frmReceivedetail
    Dim DS_Receive As New DataSet
    Dim Strsql As String
    Dim DA As New MySqlDataAdapter
    Dim DS As New DSWHIS
    Dim Mysql As New ClsSQLhelper
    Dim ClsBarcode As New Cls_Barcode
    Dim MyConn As String = Mysql.ConnMysql

    Sub Load_Data()
        Dim ColNo As New DataGridViewTextBoxColumn
        Dim ColProducts As New DataGridViewComboBoxColumn
        Dim ColQtys As New DataGridViewTextBoxColumn
        Dim colUnit As New DataGridViewTextBoxColumn

        ColNo.HeaderText = "ลำดับ"
        'ColNo.ReadOnly = True
        ColNo.Width = 40
        ColNo.DataPropertyName = "RowNo"

        ColProducts.HeaderText = "สินค้า"
        ColProducts.Width = 200
        ColProducts.DataPropertyName = "productid"
        Strsql = "Select alternatenumber,itemname from item order by itemname"
        Dim DSProducts As New DataSet
        Dim DA As New MySqlDataAdapter(Strsql, MyConn)
        DA.Fill(DS, "item")
        ColProducts.DataSource = DS.Tables("item")
        ColProducts.DisplayMember = "itemname"
        ColProducts.ValueMember = "alternatenumber"

        ColQtys.HeaderText = "จำนวน"
        ColQtys.Width = 50
        ColQtys.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        ColQtys.DataPropertyName = "Qty"

        colUnit.HeaderText = "หน่วยบรรจุ"
        colUnit.Width = 120
        colUnit.DataPropertyName = "Unit"
        'Strsql = "select  itemid,containunit from itemcontainunit  where item.itemname is not null  order by item.itemname "
        'Dim DSUnit As New DataSet
        'Dim DA2 As New MySqlDataAdapter(Strsql, MyConn)
        'DA2.Fill(DSUnit, "siteitemunit")
        'colUnit.DataSource = DSUnit.Tables("siteitemunit")
        'colUnit.DisplayMember = "unitofmeasure"
        'colUnit.ValueMember = "unitid"
        'colUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing

        With gridReceive
            .AutoGenerateColumns = False
            .Columns.Add(ColNo)
            .Columns.Add(ColProducts)
            .Columns.Add(ColQtys)
            .Columns.Add(colUnit)
            .DataSource = DS.Tables("ReceiveDetail")
        End With

        'With DataGridView1.Columns("RowNo")
        '    .HeaderText = "ลำดับ"
        '    .Width = 40
        'End With
        'With DataGridView1.Columns("productid")
        '    .HeaderText = "สินค้า"
        '    .Width = 200

        'End With

        'With DataGridView1.Columns(2)
        '    .HeaderText = "จำนวน"
        '    .Width = 50
        'End With
        'With DataGridView1.Columns(3)
        '    .HeaderText = "หน่วยบรรจุ"
        '    .Width = 120
        'End With
    End Sub
    'Private Sub TxtQty_AfterVaueChange(ByVal sender As Object, ByVal e As Custom_DGVcomboBoxColEventArgs)
    '    MsgBox(e.CurrentCellValue)
    'End Sub
    Sub Save_Data()
        For lcount As Integer = 0 To gridReceive.RowCount - 1
            Strsql = "Insert into receivingorder (receivedate,receiveby) values ('" & Pn_Framework.MysqlDateTimeFormat(Me.DTPReceivedDate.Value) & "','" & My.Settings.Currentuser & "')"
            Mysql.MySQLExecute(Strsql)

            Strsql = "Select ordernumber from receivingorder order by ordernumber desc limit 1"

            Dim NewOrder As Integer = Mysql.MySQLExecuteScalar(Strsql)
            Strsql = "Insert into receivingdetail (ordernumber,itemnumber,qty) values (" & NewOrder & ",'" & Me.gridReceive.Rows(lcount).Cells(0).Value & "'," & Me.gridReceive.Rows(lcount).Cells(2).Value & ")"
            Mysql.MySQLExecute(Strsql)
        Next
    End Sub
    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Dispose()
    End Sub

    Private Sub frmReceive_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub

    Private Sub TxtBarcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtBarcode.KeyDown
        Try
            Dim DR As DataRow
            Dim DR_NewRow As Boolean = True
            If e.KeyCode = Keys.Enter Then
                With Me.gridReceive


                    'For Each DR In DS.Tables("ReceiveDetail").Select("productid = '" & Me.txtbarcode.Text & "'")

                    '    'Strsql = "select containunit from itemcontainunit where itemid = '" & DR(1) & "'"
                    '    'Dim DSUnit As New DataSet
                    '    'Dim DAUnit As New MySqlDataAdapter(Strsql, MyConn)
                    '    'DAUnit.Fill(DSUnit, "siteitemunit")
                    '    'For i As Integer = 0 To DSUnit.Tables("siteitemunit").Rows.Count - 1
                    '    '    If DR(3) = DSUnit.Tables("siteitemunit").Rows(i).Item(0) Then

                    '    '    End If

                    '    'Next


                    '    Dim DRUnit As DataRow
                    '    For Each DRUnit In DS.Tables("ReceiveDetail").Select("Unit = '" & DR(3) & "'")

                    '    Next

                    '    'Add New Qty
                    '    DR(2) = DR(2) + 1
                    '    DR.AcceptChanges()
                    '    DR_NewRow = False
                    '    Exit For
                    'Next
                    ' If DR_NewRow = True Then
                    Dim colUnit As New DataGridViewComboBoxCell
                    'Strsql = "select  containunit from itemcontainunit where itemid = '" & .Rows(.CurrentCell.RowIndex).Cells(1).Value & "' "
                    Strsql = "select  containunit from itemcontainunit where itemid = '" & Me.TxtBarcode.Text & "' "
                    Dim DSUnit As New DataSet
                    Dim DA2 As New MySqlDataAdapter(Strsql, MyConn)
                    DA2.Fill(DSUnit, "siteitemunit")
                    ' colUnit.Items.Clear()
                    'For i As Integer = 0 To DSUnit.Tables(0).Rows.Count - 1
                    '    colUnit.Items.Add(DSUnit.Tables(0).Rows(i).Item(0))
                    'Next

                    DR = DS.Tables("ReceiveDetail").NewRow
                    DR(1) = Me.TxtBarcode.Text
                    DR(2) = 1


                    ' If .Rows.Count = 1 Then
                    '.Rows(0).Cells(3) = colUnit
                    '.Rows(0).Cells(3).Value = DSUnit.Tables(0).Rows(0).Item(0)
                    DR(3) = DSUnit.Tables(0).Rows(0).Item(0)
                    'Else
                    '.Rows(.NewRowIndex - 1).Cells(3) = colUnit
                    '.Rows(.NewRowIndex - 1).Cells(3).Value = DSUnit.Tables(0).Rows(0).Item(0)
                    'DR(3) = DSUnit.Tables(0).Rows(0).Item(0)
                    ' End If


                    DS.Tables("ReceiveDetail").Rows.Add(DR)
                    DR.AcceptChanges()


                    'End If



                End With

                TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub TxtBarcode_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TxtBarcode.MouseClick
        TxtBarcode.Select(0, TxtBarcode.Text.ToString.Length)
    End Sub


    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยัน") = MsgBoxResult.No Then Exit Sub
        Save_Data()
        If ChkPrintBarcode.Checked = True Then
            With gridReceive
                For i As Integer = 0 To Me.gridReceive.RowCount - 1
                    ClsBarcode.Print_Barcode(gridReceive.Rows(i).Cells(0).Value, gridReceive.Rows(i).Cells(1).Value)
                Next
            End With
        End If
        MsgBox("บันทึกข้อมูลเสร็จเรียบร้อยแล้ว")
    End Sub

    Private Sub gridReceive_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridReceive.CellClick
        If e.ColumnIndex = 3 Then
            Dim colUnit As New DataGridViewComboBoxCell
            Strsql = "select  containunit from itemcontainunit where itemid = '" & gridReceive.Rows(e.RowIndex).Cells(1).Value & "' "
            'Strsql = "select  containunit from itemcontainunit where itemid = '" & Me.txtbarcode.Text & "' "
            Dim DSUnit As New DataSet
            Dim DA2 As New MySqlDataAdapter(Strsql, MyConn)
            DA2.Fill(DSUnit, "siteitemunit")
            colUnit.Items.Clear()
            For i As Integer = 0 To DSUnit.Tables(0).Rows.Count - 1
                colUnit.Items.Add(DSUnit.Tables(0).Rows(i).Item(0))

            Next
            gridReceive.Rows(e.RowIndex).Cells(3) = colUnit
        End If
    End Sub
   
    Private Sub gridReceive_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridReceive.CellContentClick

    End Sub

    Private Sub gridReceive_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridReceive.CurrentCellDirtyStateChanged
        Try
            With gridReceive
                If .IsCurrentCellDirty = False Then Return

                .CommitEdit(DataGridViewDataErrorContexts.Commit)

                If .CurrentCell.ColumnIndex = 1 Then

                    Dim colUnit As New DataGridViewComboBoxCell
                    Strsql = "select  containunit from itemcontainunit where itemid = '" & .Rows(.CurrentCell.RowIndex).Cells(1).Value & "' "
                    Dim DSUnit As New DataSet
                    Dim DA2 As New MySqlDataAdapter(Strsql, MyConn)
                    DA2.Fill(DSUnit, "siteitemunit")
                    colUnit.Items.Clear()
                    For i As Integer = 0 To DSUnit.Tables(0).Rows.Count - 1
                        colUnit.Items.Add(DSUnit.Tables(0).Rows(i).Item(0))
                    Next
                    .Rows(.CurrentCell.RowIndex).Cells(3) = colUnit
                Else

                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub TxtBarcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtBarcode.TextChanged

    End Sub
End Class